package com.ps.weedingjumper.view;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.ps.weedingjumper.model.World;

public class WorldView {
	
//	private World world;
	private PlayerView playerView;
	private OrthogonalTiledMapRenderer renderer;
	private OrthographicCamera camera;
	
	public static final float SCALE = 1f;
	
	//Debug
//	private ShapeRenderer shapeRenderer = new ShapeRenderer();
	
	public WorldView(World world, PlayerView playerView) {
//		this.world = world;
		this.playerView = playerView;
		renderer = new OrthogonalTiledMapRenderer(world.getMap());
		camera = new OrthographicCamera();
		camera.position.set(Gdx.graphics.getWidth() / 2 / SCALE, Gdx.graphics.getHeight() / 2 / SCALE, 0);
		camera.viewportWidth = 800 / SCALE;  
		camera.viewportHeight = 600 / SCALE;
		camera.update();
	}
	
	public void render() {
		Gdx.gl.glClearColor(0.5f, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		renderer.setView(camera);
		renderer.render();
		
//		debug();
		
	}
	
	public void update() {
		playerView.update();
		if (playerView.getX() >= 400) {
			camera.position.set(playerView.getX() / SCALE, Gdx.graphics.getHeight() / 2 / SCALE, 0);
//			System.out.println(playerView.getX() + " " + playerView.getX() / SCALE);
			playerView.setX(400);
		} else {
			camera.position.set(Gdx.graphics.getWidth() / 2 / SCALE, Gdx.graphics.getHeight() / 2 / SCALE, 0);
//			System.out.println(Gdx.graphics.getWidth() / 2 + " " + Gdx.graphics.getWidth() / 2 / SCALE);
		}
		
		camera.update();
	}
	
	public void resize(int width, int height) {
		
	}
	
	public void dispose() {
		renderer.dispose();
		playerView.dispose();
//		shapeRenderer.dispose();
	}

//	private void debug() {
//		TiledMapTileLayer layer = (TiledMapTileLayer) world.getMap().getLayers().get(0);
//		int x = (int)((int)(playerView.getPlayer().getX() / layer.getTileWidth()) * layer.getTileWidth());
//		int y = (int)((int)(playerView.getPlayer().getY() / layer.getTileHeight()) * layer.getTileWidth());
//		
//		shapeRenderer.begin(ShapeType.Filled);
//		shapeRenderer.setColor(Color.BLACK);
//		
//		shapeRenderer.rect((x * SCALE), (y * SCALE), 1 * SCALE, 1 * SCALE);
//		shapeRenderer.rect(((x + 31) * SCALE), (y * SCALE), 1 * SCALE, 1 * SCALE);
//		
//		shapeRenderer.rect((x * SCALE), ((y + 15) * SCALE), 1 * SCALE, 1 * SCALE);
//		shapeRenderer.rect(((x + 31) * SCALE), ((y + 15) * SCALE), 1 * SCALE, 1 * SCALE);
//		
//		shapeRenderer.rect((x * SCALE), ((y + 31) * SCALE), 1 * SCALE, 1 * SCALE);
//		shapeRenderer.rect(((x + 31) * SCALE), ((y + 31) * SCALE), 1 * SCALE, 1 * SCALE);
//		
//		shapeRenderer.end();
//	}
	
}
